﻿using Microsoft.Extensions.DependencyInjection;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{
    /// <summary>
    /// 启用自动注入
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
    public class InjectionAttribute : Attribute
    {
        public InjectionAttribute() : this(null, null)
        {

        }
        public InjectionAttribute(ServiceLifetime lifetime) : this(null, lifetime)
        {

        }

        public InjectionAttribute(Type interfaceType, ServiceLifetime? lifetime = null)
        {
            this.Lifetime = lifetime;
            this.InterfaceType = interfaceType;
        }
        public ServiceLifetime? Lifetime { get; private set; }

        public int Order { get; private set; }

        public void SetImplementationType(Type implementationType)
        {
            ImplementationType = implementationType;
            if (InterfaceType == null)
            {
                InterfaceType = implementationType.GetInterfaces().FirstOrDefault(a => implementationType.Name.Contains(a.Name.TrimStart('I')));
            }
        }

        public Type InterfaceType { get; private set; }

        public Type ImplementationType { get; private set; }

    }

}
